package com.drawingtimer.admin.controller;

import com.drawingtimer.admin.model.entity.TTimeSegmentPause;
import com.drawingtimer.admin.model.qo.TTimeSegmentPauseQO;
import com.drawingtimer.admin.model.vo.TTimeSegmentPausePageVO;
import com.drawingtimer.admin.service.TTimeSegmentPauseService;
import com.hccake.ballcat.common.log.operation.annotation.CreateOperationLogging;
import com.hccake.ballcat.common.log.operation.annotation.DeleteOperationLogging;
import com.hccake.ballcat.common.log.operation.annotation.UpdateOperationLogging;
import com.hccake.ballcat.common.model.domain.PageParam;
import com.hccake.ballcat.common.model.domain.PageResult;
import com.hccake.ballcat.common.model.result.BaseResultCode;
import com.hccake.ballcat.common.model.result.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 时间段暂停记录
 *
 * @author ballcat 2025-04-30 14:13:33
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/t-time-segment-pause")
@Tag(name = "时间段暂停记录管理")
public class TTimeSegmentPauseController {

	private final TTimeSegmentPauseService tTimeSegmentPauseService;

	/**
	 * 分页查询
	 * @param pageParam 分页参数
	 * @param tTimeSegmentPauseQO 时间段暂停记录查询对象
	 * @return R 通用返回体
	 */
	@Operation(summary = "分页查询")
	@GetMapping("/page")
	@PreAuthorize("@per.hasPermission('admin:t-time-segment-pause:read')")
	public R<PageResult<TTimeSegmentPausePageVO>> getTTimeSegmentPausePage(PageParam pageParam,
			TTimeSegmentPauseQO tTimeSegmentPauseQO) {
		return R.ok(tTimeSegmentPauseService.queryPage(pageParam, tTimeSegmentPauseQO));
	}

	/**
	 * 新增时间段暂停记录
	 * @param tTimeSegmentPause 时间段暂停记录
	 * @return R 通用返回体
	 */
	@Operation(summary = "新增时间段暂停记录")
	@CreateOperationLogging(msg = "新增时间段暂停记录")
	@PostMapping
	@PreAuthorize("@per.hasPermission('admin:t-time-segment-pause:add')")
	public R<Void> save(@RequestBody TTimeSegmentPause tTimeSegmentPause) {
		return tTimeSegmentPauseService.save(tTimeSegmentPause) ? R.ok()
				: R.failed(BaseResultCode.UPDATE_DATABASE_ERROR, "新增时间段暂停记录失败");
	}

	/**
	 * 修改时间段暂停记录
	 * @param tTimeSegmentPause 时间段暂停记录
	 * @return R 通用返回体
	 */
	@Operation(summary = "修改时间段暂停记录")
	@UpdateOperationLogging(msg = "修改时间段暂停记录")
	@PutMapping
	@PreAuthorize("@per.hasPermission('admin:t-time-segment-pause:edit')")
	public R<Void> updateById(@RequestBody TTimeSegmentPause tTimeSegmentPause) {
		return tTimeSegmentPauseService.updateById(tTimeSegmentPause) ? R.ok()
				: R.failed(BaseResultCode.UPDATE_DATABASE_ERROR, "修改时间段暂停记录失败");
	}

	/**
	 * 通过id删除时间段暂停记录
	 * @param id id
	 * @return R 通用返回体
	 */
	@Operation(summary = "通过id删除时间段暂停记录")
	@DeleteOperationLogging(msg = "通过id删除时间段暂停记录")
	@DeleteMapping("/{id}")
	@PreAuthorize("@per.hasPermission('admin:t-time-segment-pause:del')")
	public R<Void> removeById(@PathVariable("id") Long id) {
		return tTimeSegmentPauseService.removeById(id) ? R.ok()
				: R.failed(BaseResultCode.UPDATE_DATABASE_ERROR, "通过id删除时间段暂停记录失败");
	}

	/**
	 * 暂停
	 */
	@PostMapping("/updatePauseTimer")
	public R<Long> pause(@RequestBody TTimeSegmentPause tTimeSegmentPause) {
		if (tTimeSegmentPauseService.getByRecordIdAndEndTimeIsNull(tTimeSegmentPause.getRecordId())) {
			return R.failed(BaseResultCode.UPDATE_DATABASE_ERROR, "暂停失败，请勿重复暂停");
		}
		return tTimeSegmentPauseService.saveAndUpdateTTimeSegmentRecord(tTimeSegmentPause)
				? R.ok(tTimeSegmentPause.getId()) : R.failed(BaseResultCode.UPDATE_DATABASE_ERROR, "暂停失败");
	}

	/**
	 * 继续
	 */
	@PostMapping("/updateResumeTimer")
	public R<Void> continuePause(@RequestBody TTimeSegmentPause tTimeSegmentPause) {
		return tTimeSegmentPauseService.updateResumeTimer(tTimeSegmentPause) ? R.ok()
				: R.failed(BaseResultCode.UPDATE_DATABASE_ERROR, "继续失败");
	}

}